package com.cat.model.biz.service.bs.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.service.bs.UserFavoriteBS;
import com.cat.model.common.enumerator.TargetType;
import com.cat.model.common.model.common.UserFavoriteVO;
import com.cat.model.dao.mapper.ModelMapper;

@Service("userFavoriteBS")
public class UserFavoriteBSImpl implements UserFavoriteBS{
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private TxCommonDAO txCommonDAO;
	
	@Override
	public UserFavoriteVO addUserFavorite(Long userId, Long targetId, TargetType targetType) {
		UserFavoriteVO vo = modelMapper.getUserFavor(userId, targetId, targetType.getValue());
		if(vo != null){
			return vo;
		}
		
		vo = new UserFavoriteVO();
		vo.setUserId(userId);
		vo.setTargetId(targetId);
		vo.setTargetType(targetType.getValue());
		vo.setCreateTime(new Date());
		
		txCommonDAO.insert(vo);
		
		return vo;
	}
	
	@Override
	public boolean isUserFavorite(Long userId, Long targetId, TargetType targetType) {
		UserFavoriteVO vo = modelMapper.getUserFavor(userId, targetId, targetType.getValue());
		if(vo != null){
			return true;
		}
		return false;
	}

	@Override
	public boolean delUserFavorite(Long userId, Long targetId, TargetType targetType) {
		UserFavoriteVO vo = modelMapper.getUserFavor(userId, targetId, targetType.getValue());
		if(vo == null){
			return true;
		}
		
		int cnt = txCommonDAO.deleteById(vo);
		
		return cnt > 0 ? true : false;
	}

	@Override
	public List<UserFavoriteVO> getUserFavoriteList(Long userId, TargetType targetType) {
		 List<UserFavoriteVO> list = modelMapper.getUserFavorList(userId, targetType.getValue());
		 
		return list;
	}

	@Override
	public int getTargetFavoriteCount(Long targetId, TargetType targetType) {
		 return modelMapper.getTargetFavoriteCount(targetId, targetType.getValue());
	}
}
